﻿#include<stdio.h>
#include<stdlib.h>
#include<conio.h>
#include<Windows.h>
#define HIGH 18	//游戏画面大小
#define WIDTH 30
#define enemynum 5//敌机数量
//全局变量
int canvas[HIGH][WIDTH]= { 0 };			//0则不输出，1则输出玩机，2输出敌机，3输出子弹
int self_x, self_y;						//玩家飞机位置
int enemy_x[enemynum], enemy_y[enemynum];//敌机位置
int bullet_x, bullet_y;					//子弹位置
int score,sub_score;								//得分
int bulletwidth;						//子弹宽度
void gotoxy(int x, int y)				//光标移动到(x,y)位置
{
	HANDLE handle = GetStdHandle(STD_OUTPUT_HANDLE);
	COORD pos;
	pos.X = x;
	pos.Y = y;
	SetConsoleCursorPosition(handle, pos);
}

void HideCursor()				//隐藏光标
{
	CONSOLE_CURSOR_INFO cursor_info = { 1, 0 };
	SetConsoleCursorInfo(GetStdHandle(STD_OUTPUT_HANDLE), &cursor_info);
}

void startup()					//游戏开始初始化
{
	self_x = HIGH-4;
	self_y = WIDTH / 2;
	int k;
	for (k = 0; k < enemynum; k++)
	{
		enemy_x[k] = rand()%3;
		enemy_y [k]= rand() % WIDTH;
		canvas[enemy_x[k]][enemy_y[k]] = 2;
	}
	score =sub_score =0;
	canvas[self_x][self_y] = 1;
	canvas[bullet_x][bullet_y] = 0;
	bulletwidth = 0;
}

void show()											//画面显示		
{
	gotoxy(0, 0);									// 光标移动到原点位置，以下重画清屏
	for (int i = 0; i < HIGH ; i++)
	{
		for (int j = 0; j < WIDTH-1 ; j++)
		{
			if (canvas[i][j]==1)
				printf("0");		//输出玩机
			else if (canvas[i][j] == 2)
				printf("*");		//输出敌机
			else if (canvas[i][j] == 3)
				printf("|");		//输出子弹
			else
				printf(" ");
		}
		printf("\n");
	}
	printf("得分：%d\n", score);
}

void update_with_input()					//与用户输入有关的更新
{
	char input;
	if (_kbhit())
	{
		input = _getch();
		if (input == 'a')
		{
			if (self_y >=0 && self_y < WIDTH)
			{
				canvas[self_x][self_y] = 0;
				self_y--;
				canvas[self_x][self_y] = 1;
			}
		}
		if (input == 'd')
		{
			if (self_y >= 0 && self_y <= WIDTH)
			{
				canvas[self_x][self_y] = 0;
				self_y++;
				canvas[self_x][self_y] = 1;
			}
		}
		if (input == ' ')
		{
			int x, left, right;
			left = self_y - bulletwidth;
			right = self_y + bulletwidth;
			for(x=left;x<=right;x++)
				canvas[self_x-1][x] = 3;
		}
	}
}

void update_without_input()					//与用户输入无关的更新
{
	for (int i = 0; i < HIGH; i++)
	{
		for (int j = 0; j < WIDTH; j++)
		{
			if (canvas[i][j] == 3)
			{
				for (int k = 0; k < enemynum; k++)
				{
					if (i == enemy_x[k] && j == enemy_y[k])
					{
						canvas[enemy_x[k]][enemy_y[k]] = 0;
						enemy_x[k] = 1;
						enemy_y[k] = rand() % WIDTH;
						canvas[enemy_x[k]][enemy_y[k]] = 2;
						score++;
						sub_score++;
					}
				}
				canvas[i][j] = 0;
				if (i > 0)
					canvas[i - 1][j] = 3;
			}
		}
	}int static speed = 0;//速度控制
	if (speed < 30)
	{
		speed++;
	}
	if (speed == 30)
	{
		int k;
		for (k = 0; k < enemynum; k++)
		{
			if (enemy_x[k] < HIGH)//未越界
			{
				canvas[enemy_x[k]][enemy_y[k]] = 0;
				enemy_x[k]++;
				canvas[enemy_x[k]][enemy_y[k]] = 2;
			}
			else//越界
			{
				canvas[enemy_x[k]][enemy_y[k]] = 0;
				enemy_x[k] = 0;
				enemy_y[k] = rand() % WIDTH;
				canvas[enemy_x[k]][enemy_y[k]] = 2;
				score--;
				score--;
				sub_score--;
			}
		}
		speed = 0;
	}
	if (bulletwidth < 5)
	{
		if (sub_score == 5)
		{
			bulletwidth = bulletwidth + (sub_score / 5);
			sub_score = 0;
		}
	}
}

int main()
{
	startup();

	while (1)
	{
		HideCursor();					//隐藏光标

		show();							//游戏开始初始化


		update_with_input();			//与用户输入无关的更新

		update_without_input();			//与用户输入有关的更新
	}
	return 0;
}
